package com.guwu.compress_image_uploading;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.guwu.compress_image_uploading.utils.CompressUtils;
import com.guwu.compress_image_uploading.utils.PhotoOrCropUtil;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.camera)
    Button camera;
    @BindView(R.id.single_gallery)
    Button singleGallery;
    @BindView(R.id.multi_gallery)
    Button multiGallery;
    @BindView(R.id.image_layout)
    LinearLayout imageLayout;
    @BindView(R.id.img)
    ImageView img;


    private PhotoOrCropUtil photoOrCropUtil;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        photoOrCropUtil = new PhotoOrCropUtil(this);
        photoOrCropUtil.setPhotoOrCropListener(new PhotoOrCropUtil.PhotoOrCropListener() {
            @Override
            public void onSuccess(File file) {
                //file是没有压缩的图片，如果需要压缩就compressPic
                compressPic(file);
            }

            @Override
            public void onSuccess(List<File> files) {
                //files是没有压缩的图片列表，如果需要压缩就compressPics
                compressPics(files);
            }

            @Override
            public void onError(String error) {
                showToast(error);
            }
        });
    }

    private void compressPics(List<File> files) {
        photoOrCropUtil.compressPic(files, new CompressUtils.CompressFinishCallBack<List<File>>() {
            @Override
            public void compressStart(String msg) {
                //压缩前
            }

            @Override
            public void compressFinish(List<File> files) {
                //压缩完成
                addImageViews(files);
            }

            @Override
            public void compressError(String msg) {
                showToast(msg);
            }
        });
    }

    private void compressPic(File file) {
        photoOrCropUtil.compressPic(file, new CompressUtils.CompressFinishCallBack<File>() {
            @Override
            public void compressStart(String msg) {
                //压缩前
            }

            @Override
            public void compressFinish(File files) {
                //压缩完成
                addImageView(files);
            }

            @Override
            public void compressError(String msg) {
                showToast(msg);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        photoOrCropUtil.onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    @OnClick({R.id.camera, R.id.single_gallery, R.id.multi_gallery})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.camera:
                photoOrCropUtil.openCamera(false);
                break;
            case R.id.single_gallery:
                photoOrCropUtil.openGallerySingle(false);
                break;
            case R.id.multi_gallery:
                photoOrCropUtil.openGalleryMuti(9);
                break;
        }
    }

    private void addImageViews(List<File> files) {
        for (File f : files) {
            addImageView(f);
        }
    }

    private void addImageView(File file) {
        ImageView view = new ImageView(this);
        view.setLayoutParams(new LinearLayout.LayoutParams(200, 200));
        view.setImageURI(Uri.fromFile(file));
        imageLayout.addView(view);
        img.setImageURI(Uri.fromFile(file));
    }

    private void showToast(String text) {
        Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
    }


}
